﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDToaThuocNgoaiVien
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private long _deidql;
        private int _iidbacsi;
        private int _iiddangnhap;
        private int _iidphong;
        private string _schandoan;
        private string _sghichu;
        private string _smabn;
        private string _sngay;
        private string _sngaycn;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDToaThuocNgoaiVien()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDToaThuocNgoaiVien(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocNgoaiVien.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDToaThuocNgoaiVien.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDToaThuocNgoaiVien.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVien.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocNgoaiVien.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDQL, ",", columnMABN, ",", columnNGAY, ",", columnCHANDOAN, ",", columnIDPHONG, 
                    ",", columnGHICHU, ",", columnIDBACSI, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ") values(", this._deid, ",", this._deidql, ",'", this._smabn, "',to_timestamp('", this._sngay, 
                    "','dd/mm/yyyy hh24:mi'),:", columnCHANDOAN, ",", this._iidphong, ",:", columnGHICHU, ",", this._iidbacsi, ",", this._iiddangnhap, ",:", columnTENMAYTINH, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCHANDOAN, NpgsqlDbType.Text, 200).Value = this._schandoan;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDToaThuocNgoaiVien.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidql = 0L;
            this._smabn = "";
            this._sngay = "";
            this._schandoan = "";
            this._iidphong = 0;
            this._sghichu = "";
            this._iidbacsi = 0;
            this._iiddangnhap = 0;
            this._stenmaytinh = "";
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDToaThuocNgoaiVien.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDQL, ",", columnMABN, ",to_char(", columnNGAY, ",'dd/mm/yyyy hh24:mi') as ", columnNGAY, ",", columnCHANDOAN, ",", columnIDPHONG, ",", columnGHICHU, 
                    ",", columnIDBACSI, ",", columnIDDANGNHAP, ",", columnTENMAYTINH, ",to_char(", columnNGAYCN, ",'dd/mm/yyyy hh24:mi') as ", columnNGAYCN, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    " = ", this._deid
                 }), connect).Fill(dataTable);
                this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                this._deidql = long.Parse(dataTable.Rows[0][columnIDQL].ToString());
                this._smabn = dataTable.Rows[0][columnMABN].ToString();
                this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                this._schandoan = dataTable.Rows[0][columnCHANDOAN].ToString();
                this._iidphong = int.Parse(dataTable.Rows[0][columnIDPHONG].ToString());
                this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                this._iidbacsi = int.Parse(dataTable.Rows[0][columnIDBACSI].ToString());
                this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVien.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDToaThuocNgoaiVien.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDQL + "," + columnMABN + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnCHANDOAN + "," + columnIDPHONG + "," + columnGHICHU + "," + columnIDBACSI + "," + columnIDDANGNHAP + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy hh24:mi') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " order by " + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDToaThuocNgoaiVien.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocNgoaiVien.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDQL, " = ", this._deidql, ",", columnMABN, " = '", this._smabn, "',", columnNGAY, " = to_timestamp('", this._sngay, 
                    "','dd/mm/yyyy hh24:mi'),", columnCHANDOAN, " = :", columnCHANDOAN, ",", columnIDPHONG, " = ", this._iidphong, ",", columnGHICHU, " = :", columnGHICHU, ",", columnIDBACSI, " = ", this._iidbacsi, 
                    ",", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, ",", columnNGAYCN, " = now()  where ", columnID, " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnCHANDOAN, NpgsqlDbType.Text, 200).Value = this._schandoan;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDToaThuocNgoaiVien.cs");
                return false;
            }
        }

        public static string columnCHANDOAN
        {
            get
            {
                return "chandoan";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDBACSI
        {
            get
            {
                return "idbacsi";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDPHONG
        {
            get
            {
                return "idphong";
            }
        }

        public static string columnIDQL
        {
            get
            {
                return "idql";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        private string NGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public string pCHANDOAN
        {
            get
            {
                return this._schandoan;
            }
            set
            {
                this._schandoan = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDBACSI
        {
            get
            {
                return this._iidbacsi;
            }
            set
            {
                this._iidbacsi = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDPHONG
        {
            get
            {
                return this._iidphong;
            }
            set
            {
                this._iidphong = value;
            }
        }

        public long pIDQL
        {
            get
            {
                return this._deidql;
            }
            set
            {
                this._deidql = value;
            }
        }

        public string pMABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_toathuoc_ngoaivien";
            }
        }
    }
}

